package com.example.dilidili_ting.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dilidili_ting.R;
import com.example.dilidili_ting.bean.SetSecurityBeanTing;
import com.example.dilidili_ting.bean.loginResultBeanTing;
import com.example.dilidili_ting.utils.OkhttpUtilsTing;
import com.example.dilidili_ting.utils.SharedPreferencesUtilsTing;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.thecode.aestheticdialogs.AestheticDialog;
import com.thecode.aestheticdialogs.DialogStyle;
import com.thecode.aestheticdialogs.DialogType;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class FindPswActivityTing extends AppCompatActivity {

    private ImageView ivBack2Ting;
    private TextView titleBarNameTing;
    private ImageView ivXiaokeaiTing;
    private EditText editZhmmUsernameTing;
    private EditText editZhmmNameTing;
    private TextView zhmmMmTing;
    private Button btnZhmmTing;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_psw_ting);
        initView();
    }

    private void initView() {
        ivBack2Ting = (ImageView) findViewById(R.id.iv_back2_ting);
        titleBarNameTing = (TextView) findViewById(R.id.title_bar_name_ting);
        ivXiaokeaiTing = (ImageView) findViewById(R.id.iv_xiaokeai_ting);
        editZhmmUsernameTing = (EditText) findViewById(R.id.edit_zhmm_username_ting);
        editZhmmNameTing = (EditText) findViewById(R.id.edit_zhmm_name_ting);
        zhmmMmTing = (TextView) findViewById(R.id.zhmm_mm_ting);
        btnZhmmTing = (Button) findViewById(R.id.btn_zhmm_ting);
        titleBarNameTing.setText("找回密码");
        ivXiaokeaiTing.setBackgroundResource(R.drawable.boy);
        ivBack2Ting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FindPswActivityTing.this.finish();
            }
        });
        //按钮点击事件
        btnZhmmTing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btnclick();
            }
        });
    }

    private void btnclick() {
        String validateName_ting = editZhmmNameTing.getText().toString().trim();
        final String userName_ting = editZhmmUsernameTing.getText().toString().trim();
        String sp_security = SharedPreferencesUtilsTing.readSecurity(FindPswActivityTing.this,userName_ting);
        if(TextUtils.isEmpty(userName_ting)){
            new AestheticDialog.Builder(this, DialogStyle.TOASTER, DialogType.WARNING)
                    .setTitle("WARNING")
                    .setMessage("请输入您的用户名")
                    .setDuration(1500)
                    .show();
            return;
        }else if(TextUtils.isEmpty(validateName_ting)){
            new AestheticDialog.Builder(this, DialogStyle.TOASTER, DialogType.WARNING)
                    .setTitle("WARNING")
                    .setMessage("请输入要验证的姓名")
                    .setDuration(1500)
                    .show();
            return;
        }else if(!SharedPreferencesUtilsTing.isExistUserName(FindPswActivityTing.this,userName_ting)){
            new AestheticDialog.Builder(this, DialogStyle.TOASTER, DialogType.ERROR)
                    .setTitle("ERROR")
                    .setMessage("您输入的用户名不存在")
                    .setDuration(1500)
                    .show();
            return;
        }else if (!validateName_ting.equals(sp_security)){
            new AestheticDialog.Builder(this, DialogStyle.TOASTER, DialogType.ERROR)
                    .setTitle("ERROR")
                    .setMessage("输入的密保不正确")
                    .setDuration(1500)
                    .show();
            return;
        }else {
            //输入密保正确，重新给用户设置一个密码
            JsonObject jo = new JsonObject();
            jo.addProperty("userName",userName_ting);
//            jo.addProperty("kfz","zxt");
            jo.addProperty("answer",validateName_ting);
            OkhttpUtilsTing.isPOST("AndroidUser/resetPwd",false, jo, new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {}

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String res = response.body().string();
                    Gson gson = new Gson();
                    final loginResultBeanTing<SetSecurityBeanTing> r = gson.fromJson(res, loginResultBeanTing.class);
                    FindPswActivityTing.this.runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            if(r.code == 200){
//                                zhmmMmTing.setVisibility(View.VISIBLE);
//                              zhmmMmTing.setText("初始密码：123456");
                                new AestheticDialog.Builder(FindPswActivityTing.this, DialogStyle.EMOTION, DialogType.SUCCESS)
                                        .setTitle("SUCCESS")
                                        .setMessage("找回密码成功，初始密码为123456")
                                        .setDuration(2200)
                                        .show();
                                SharedPreferencesUtilsTing.savePsw(FindPswActivityTing.this,userName_ting);
                                //设置跳转时长
                                Timer timer=new Timer();
                                TimerTask task=new TimerTask() {
                                    @Override
                                    public void run() {
                                        Intent data = new Intent();
                                        data.putExtra("userName",userName_ting);
                                        data.putExtra("password","123456");
                                        setResult(RESULT_OK,data);
                                        FindPswActivityTing.this.finish();
                                    }
                                };
                                timer.schedule(task,2200);
                            }else {
                                new AestheticDialog.Builder(FindPswActivityTing.this, DialogStyle.EMOTION, DialogType.ERROR)
                                        .setTitle("ERROR")
                                        .setMessage(r.msg)
                                        .setDuration(1500)
                                        .show();
                            }
                        }
                    });
                }
            });
        }
    }

}